package com.lancoo.edu.platform.operation.commons.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author lzh
 * @description json文件读取工具类
 */
@Slf4j
public class JsonFileUtil {



    /**
     * 把指定对象写入文件中
     *
     * @param target     目标对象
     * @param folderPath 文件夹地址
     * @param fileName   文件名称
     */
    public static String jsonWrite(Object target, String folderPath, String fileName){
        // 1、创建文件夹
        File dir = new File(folderPath);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                throw new RuntimeException("文件夹创建失败");
            }
        }
        // 2、写入文件
        OutputStreamWriter outputStreamWriter = null;
        try {
            outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(Paths.get(folderPath + fileName).toFile().toPath()), StandardCharsets.UTF_8);
            String string = JSONObject.toJSONString(target);
            outputStreamWriter.write(string);
            outputStreamWriter.flush();
            outputStreamWriter.close();
        } catch (Exception e) {
            log.error("【写入Json文件失败】", e);
        } finally {
            if (outputStreamWriter != null) {
                try {
                    outputStreamWriter.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return folderPath + "/" + fileName;
    }

    /**
     * 将上传的JSON文件转换为对应Java对象
     *
     * @param file 上传的JSON文件
     * @return json对象
     */
    public static <T> T getJsonToObject(MultipartFile file, Class<T> targetType){
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            return JSONObject.parseObject(inputStream, targetType);
        } catch (Exception e) {
            log.error("【JSON文件解析出错，文件：{}】", file);
            e.printStackTrace();
        } finally {
            // 关闭流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * JSON文件转换为对应Java对象
     * @param jsonUrl json文件地址
     * @return json对象
     */
    public static <T> T getJsonToObject(String jsonUrl,Class<T> targetType){
        ClassPathResource classPathResource = new ClassPathResource(jsonUrl);
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getStream();
            return JSONObject.parseObject(inputStream,targetType);
        }catch (Exception e){
            log.error("【JSON文件解析出错，地址：{}】",jsonUrl);
            e.printStackTrace();
        }finally {
            // 关闭流
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * JSON文件转换为对应List对象
     * @param jsonUrl json文件地址
     * @return json对象
     */
    public static <T> List<T> getJsonToArray(String jsonUrl, Class<T> targetType){
        ClassPathResource classPathResource = new ClassPathResource(jsonUrl);
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getStream();
            return JSONObject.parseArray(IOUtils.toString(inputStream,"utf-8"), targetType);
        }catch (Exception e){
            log.error("【JSON文件解析出错，地址：{}】",jsonUrl);
            e.printStackTrace();
        }finally {
            // 关闭流
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
